package com.smart.base.tools.generator.service;

import java.io.File;
import java.util.Map;

import com.google.common.collect.Maps;
import com.smart.base.tools.generator.util.TemplateUtil;
import com.smart.base.utils.StringUtil;

/**
 * @ClassName：GenerateMain @Description：
 * @author：shisongbin
 * @date：2016年5月24日 下午4:45:58
 */
public class GenerateMain {
	
	/**这个不改**/
	private static final String  FTL_PACK_PATH ="/generator/service";
	
	/**个人作者**/
	public static final String AUTHOR_BY = "wangshaokui@ssit-xm.com.cn";
	/**项目版本**/
	public static final String VERSION = "0.0.1-SNAPSHOT";
	/**服务归属 公共common - 业务 biz**/
	public static final String SERVER_TYPE = "common";
	
	/** 代码生成项目路径 */
	public static final String CODE_PROJECT_PATH = "D:/company/qx/project/workspace3/smart-server/smart-server-"+SERVER_TYPE+"/";

	/**服务名称**/
	public static final String SERVER_NAME = "定时服务";
	public static final String SERVER_CODE = "task";
	public static final String DUBBO_PORT = "21824";
	
	/**创建基础路径**/
	private static void mkdirBase(){
		//创建文件路径
		String parentServer = "smart-server-"+ SERVER_TYPE;
		String serverCode = "smart-server-"+ SERVER_TYPE + "-"+SERVER_CODE;
		Map params = Maps.newHashMap();
		
		//生成.settings
		params.clear();
		TemplateUtil.create(FTL_PACK_PATH+"/base/settings", "org.eclipse.m2e.core.prefs.ftl",CODE_PROJECT_PATH +"/"+serverCode+"/.settings/org.eclipse.m2e.core.prefs" + "" , params);
				
		//生成.project
		params.clear();
		params.put("serverCode", serverCode);
		TemplateUtil.create(FTL_PACK_PATH +"/base", "project.ftl",CODE_PROJECT_PATH +"/"+serverCode+"/.project" + "" , params);
		
		//生成pom
		params.clear();
		params.put("parentServer", parentServer);
		params.put("serverCode", serverCode);
		params.put("serverName", SERVER_NAME);
		TemplateUtil.create(FTL_PACK_PATH+"/base", "pom.ftl",CODE_PROJECT_PATH +"/"+serverCode+"/pom.xml" + "" , params);
		
	}
	
	/**创建api路径**/
	private static void mkdirApi(){
		//创建文件路径
		String parentServer =  "smart-server-"+ SERVER_TYPE + "-"+SERVER_CODE;
		String serverCode = "smart-server-"+ SERVER_TYPE + "-"+SERVER_CODE+"-api";
		Map params = Maps.newHashMap();
		
		//生成.settings
		params.clear();
		TemplateUtil.create(FTL_PACK_PATH+"/api/settings", "org.eclipse.core.resources.prefs.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/.settings/org.eclipse.core.resources.prefs" , params);
		TemplateUtil.create(FTL_PACK_PATH+"/api/settings", "org.eclipse.jdt.core.prefs.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/.settings/org.eclipse.jdt.core.prefs" , params);
		TemplateUtil.create(FTL_PACK_PATH+"/api/settings", "org.eclipse.m2e.core.prefs.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/.settings/org.eclipse.m2e.core.prefs" , params);
		
		//生成src-main-java目录
		String srcMainJavaPath = CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/src/main/java/com/smart/server/"+SERVER_TYPE+"/"+SERVER_CODE;
		if(!new File(srcMainJavaPath+"/dto").exists()) new File(srcMainJavaPath+"/dto").mkdirs();
		if(!new File(srcMainJavaPath+"/job").exists()) new File(srcMainJavaPath+"/job").mkdirs();
		if(!new File(srcMainJavaPath+"/exception").exists()) new File(srcMainJavaPath+"/exception").mkdirs();
		if(!new File(srcMainJavaPath+"/api").exists()) new File(srcMainJavaPath+"/api").mkdirs();
		
		
		//生成Exceptionr
		params.clear();
		params.put("serverType", SERVER_TYPE);
		params.put("serverCode", SERVER_CODE);
		TemplateUtil.create(FTL_PACK_PATH+"/api", "exception.java.ftl",srcMainJavaPath+"/exception/"+StringUtil.toUpperCaseFirstOne(SERVER_CODE)+"Exception.java", params);
		
		
		//生成src-test-java目录
		String srcTestJavaPath = CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/src/test/java";
		if(!new File(srcTestJavaPath).exists()) new File(srcTestJavaPath).mkdirs();
		
		//生成.classpath
		params.clear();
		TemplateUtil.create(FTL_PACK_PATH+"/api", "classpath.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/.classpath" ,params);
		params.clear();
		params.put("serverCode", serverCode);
		TemplateUtil.create(FTL_PACK_PATH+"/api", "project.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/.project" ,params);
		params.clear();
		params.put("version", VERSION);
		params.put("serverName", SERVER_NAME);
		params.put("serverCode", serverCode);
		TemplateUtil.create(FTL_PACK_PATH+"/api", "pom.xml.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/pom.xml" ,params);
	}
	
	
	/**创建service路径**/
	private static void mkdirService(){
		//创建文件路径
		String parentServer =  "smart-server-"+ SERVER_TYPE + "-"+SERVER_CODE;
		String serverCode = "smart-server-"+ SERVER_TYPE + "-"+SERVER_CODE+"-service";
		Map params = Maps.newHashMap();
		
		//生成.settings
		params.clear();
		TemplateUtil.create(FTL_PACK_PATH+"/service/settings", "org.eclipse.core.resources.prefs.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/.settings/org.eclipse.core.resources.prefs" , params);
		TemplateUtil.create(FTL_PACK_PATH+"/service/settings", "org.eclipse.jdt.core.prefs.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/.settings/org.eclipse.jdt.core.prefs" , params);
		TemplateUtil.create(FTL_PACK_PATH+"/service/settings", "org.eclipse.m2e.core.prefs.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/.settings/org.eclipse.m2e.core.prefs" , params);
		
		//生成 assembly.xml
		params.clear();
		TemplateUtil.create(FTL_PACK_PATH+"/service/java", "assembly.xml.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/src/main/assembly/assembly.xml" , params);
		//生成常量
		params.clear();
		params.put("serverType", SERVER_TYPE);
		params.put("serverCode", SERVER_CODE);
		TemplateUtil.create(FTL_PACK_PATH+"/service/java", "conts.java.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/src/main/java/com/smart/server/"+SERVER_TYPE+"/"+SERVER_CODE+"/conts/"+ StringUtil.toUpperCaseFirstOne(SERVER_CODE)+"Conts.java", params);
		//生成常量
		params.clear();
		params.put("author", AUTHOR_BY);
		params.put("serverType", SERVER_TYPE);
		params.put("serverCode", SERVER_CODE);
		TemplateUtil.create(FTL_PACK_PATH+"/service/java", "errorCode.java.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/src/main/java/com/smart/server/"+SERVER_TYPE+"/"+SERVER_CODE+"/conts/"+ "ErrorCode.java", params);
		//生成service目录
		String srcMainJavaPath = CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/src/main/java/com/smart/server/"+SERVER_TYPE+"/"+SERVER_CODE+"/service";
		if(!new File(srcMainJavaPath).exists()) new File(srcMainJavaPath).mkdirs();
		
		//生成resources/config_filter/env_dev.properties
		params.clear();
		params.put("appName", parentServer);
		params.put("codeProjectPath", CODE_PROJECT_PATH +parentServer+"/"+serverCode);
		TemplateUtil.create(FTL_PACK_PATH+"/service/resources/config_filter", "env_dev.properties.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/src/main/resources/config_filter/env_dev.properties", params);
		
		//生成resources/config_filter/env_test.properties
		params.clear();
		params.put("appName", parentServer);
		params.put("serverType", SERVER_TYPE);
		TemplateUtil.create(FTL_PACK_PATH+"/service/resources/config_filter", "env_test.properties.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/src/main/resources/config_filter/env_test.properties", params);
		//生成resources/config_filter/env_test.properties
		params.clear();
		TemplateUtil.create(FTL_PACK_PATH+"/service/resources/config_filter", "env_prod-pre.properties.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/src/main/resources/config_filter/env_prod-pre.properties", params);
		//生成resources/config_filter/env_test.properties
		params.clear();
		TemplateUtil.create(FTL_PACK_PATH+"/service/resources/config_filter", "env_prod.properties.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/src/main/resources/config_filter/env_prod.properties", params);
	
		//生成resources/META-INF/dubbo
		params.clear();
		TemplateUtil.create(FTL_PACK_PATH+"/service/resources/META-INF", "com.alibaba.dubbo.rpc.Filter.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/src/main/resources/META-INF/dubbo/com.alibaba.dubbo.rpc.Filter", params);
		//生成resources/META-INF/spring
		params.clear();
		TemplateUtil.create(FTL_PACK_PATH+"/service/resources/META-INF", "spring-context.xml.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/src/main/resources/META-INF/spring/spring-context.xml", params);
		TemplateUtil.create(FTL_PACK_PATH+"/service/resources/META-INF", "spring-dubbo.xml.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/src/main/resources/META-INF/spring/spring-dubbo.xml", params);
		TemplateUtil.create(FTL_PACK_PATH+"/service/resources/META-INF", "spring-dubbo-consumer.xml.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/src/main/resources/META-INF/spring/spring-dubbo-consumer.xml", params);
		TemplateUtil.create(FTL_PACK_PATH+"/service/resources/META-INF", "spring-dubbo-provider.xml.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/src/main/resources/META-INF/spring/spring-dubbo-provider.xml", params);
		TemplateUtil.create(FTL_PACK_PATH+"/service/resources/META-INF", "spring-mybatis.xml.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/src/main/resources/META-INF/spring/spring-mybatis.xml", params);
		
		//生成SqlMapConfig.xml
		params.clear();
		TemplateUtil.create(FTL_PACK_PATH+"/service/resources/mybatis", "SqlMapConfig.xml.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/src/main/resources/mybatis/SqlMapConfig.xml", params);

		//生成properties
		params.clear();
		TemplateUtil.create(FTL_PACK_PATH+"/service/resources/properties", "application.properties.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/src/main/resources/properties/application.properties", params);
		params.clear();
		params.put("authorBy", AUTHOR_BY);
		params.put("dubboPort", DUBBO_PORT);
		TemplateUtil.create(FTL_PACK_PATH+"/service/resources/properties", "dubbo.properties.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/src/main/resources/properties/dubbo.properties", params);
		params.clear();
		TemplateUtil.create(FTL_PACK_PATH+"/service/resources/properties", "exception.properties.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/src/main/resources/properties/exception.properties", params);
		params.clear();
		TemplateUtil.create(FTL_PACK_PATH+"/service/resources/properties", "jdbc.properties.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/src/main/resources/properties/jdbc.properties", params);
		params.clear();
		TemplateUtil.create(FTL_PACK_PATH+"/service/resources", "logback.xml.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/src/main/resources/logback.xml", params);
		
		//生成test
		params.clear();
		params.put("serverType", SERVER_TYPE);
		params.put("serverCode", SERVER_CODE);
		TemplateUtil.create(FTL_PACK_PATH+"/service/test", "Provider.java.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/src/test/java/com/smart/server/"+SERVER_TYPE+"/"+SERVER_CODE+"/service/"+StringUtil.toUpperCaseFirstOne(SERVER_CODE)+"Provider.java", params);
		
		//.classpath
		params.clear();
		TemplateUtil.create(FTL_PACK_PATH+"/service", "classpath.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/.classpath", params);
		params.clear();
		params.put("serverCode", serverCode);
		TemplateUtil.create(FTL_PACK_PATH+"/service", "project.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/.project", params);
		params.clear();
		params.put("appName", parentServer);
		params.put("serverType", SERVER_TYPE);
		params.put("serverCode", SERVER_CODE);
		params.put("serverName", SERVER_NAME);
		TemplateUtil.create(FTL_PACK_PATH+"/service", "pom.xml.ftl",CODE_PROJECT_PATH +"/"+parentServer+"/"+serverCode+"/pom.xml", params);
	}
	public static void main(String[] args) {
		mkdirBase();
		mkdirApi();
		mkdirService();
		System.out.println("生成完成!");
	}
}
